package com.yangfei.mystudy1020.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringChain;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.yangfei.mystudy1020.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KRadioActivity extends Activity {

    private static final String TAG = "KRadioActivity";
    private static final int ROTATE_90_HALF = 1002;
    private static final int ROTATE_90N = 1001;
    private static final int ROTATE_90 = 1000;

    final int PLAYERBAR_SHOW = 100;
    final int PLAYERBAR_HIDE = 101;

    final int ANIM_TIME = 500;

    @BindView(R.id.tv_item1)
    TextView tvItem1;
    @BindView(R.id.tv_item2)
    TextView tvItem2;
    @BindView(R.id.tv_item3)
    TextView tvItem3;
    @BindView(R.id.tv_item4)
    TextView tvItem4;
    @BindView(R.id.tv_item5)
    TextView tvItem5;
    @BindView(R.id.tv_item6)
    TextView tvItem6;
    @BindView(R.id.tv_item7)
    TextView tvItem7;
    @BindView(R.id.ll_item)
    LinearLayout llItem;
    @BindView(R.id.ll_plaerbar)
    LinearLayout llPlaerbar;
    @BindView(R.id.iv_btn)
    ImageView ivBtn;
    @BindView(R.id.iv_cover)
    ImageView ivCover;

    private int playerBarWidth;
    private Spring springPlayerBar;

    int mPlayerBarState = PLAYERBAR_SHOW;
    private int coverWidth;

    ArrayList<View> mViewArray = new ArrayList<>();
    private final SpringChain mSpringChain = SpringChain.create(30, 6, 70, 7);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kradio);
        ButterKnife.bind(this);
        initSpring();
        initSpringChain();
        ivBtn.setOnClickListener(v -> {
            if (mPlayerBarState == PLAYERBAR_SHOW) {
                updateSpring(PLAYERBAR_HIDE);
                flip(ivBtn, ANIM_TIME, -1,ROTATE_90);
                flip(tvItem1, ANIM_TIME, -1,ROTATE_90N);
            }
        });

        ivCover.setOnClickListener(v -> {
            if (mPlayerBarState == PLAYERBAR_HIDE) {
                updateSpring(PLAYERBAR_SHOW);
                ivCover.postDelayed(() -> {
                    flip(ivBtn, ANIM_TIME, -1,ROTATE_90_HALF);
                },ANIM_TIME/2);

                flip(tvItem1, ANIM_TIME, -1,ROTATE_90);
            }
        });
        tvItem1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"click view");
            }
        });
    }

    //动画
    private void initSpring() {
        llPlaerbar.post(() -> {
            playerBarWidth = llPlaerbar.getWidth();
            coverWidth = ivCover.getWidth();
            Log.d(TAG, "initSpring playerBarWidth=" + playerBarWidth + " coverWidth=" + coverWidth);
        });
        SpringSystem springSystem = SpringSystem.create();
        springPlayerBar = springSystem.createSpring();
        springPlayerBar
                .setSpringConfig(new SpringConfig(70, 12.9))
                .addListener(new SimpleSpringListener() {
                    @Override
                    public void onSpringUpdate(Spring spring) {
                        float value = (float) spring.getCurrentValue();
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llPlaerbar.getLayoutParams();
                        switch (mPlayerBarState) {
                            case PLAYERBAR_SHOW: {
                                params.width = (int) value;
                                params.setMargins((int) (playerBarWidth - value), 0, 0, 0);
                                llPlaerbar.setLayoutParams(params);

                                mSpringChain
                                        .setControlSpringIndex(mViewArray.size() - 3)
                                        .getControlSpring()
                                        .setEndValue(0);
                                break;
                            }
                            case PLAYERBAR_HIDE: {
                                params.width = (int) value;
                                params.setMargins((int) (playerBarWidth - value), 0, 0, 0);
                                llPlaerbar.setLayoutParams(params);

                                mSpringChain
                                        .setControlSpringIndex(1)
                                        .getControlSpring()
                                        .setEndValue(1);
                                break;
                            }
                        }
                    }
                });
    }

    private void updateSpring(int state) {
        mPlayerBarState = state;
        switch (state) {
            case PLAYERBAR_SHOW: {
                springPlayerBar.setCurrentValue(coverWidth);
                springPlayerBar.setEndValue(playerBarWidth);
                break;
            }
            case PLAYERBAR_HIDE: {
                springPlayerBar.setCurrentValue(playerBarWidth);
                springPlayerBar.setEndValue(coverWidth);
                break;
            }
        }
    }

    private void initSpringChain() {
        mViewArray.add(tvItem2);
        mViewArray.add(tvItem3);
        mViewArray.add(tvItem4);
        mViewArray.add(tvItem5);
        mViewArray.add(tvItem6);
        mViewArray.add(tvItem7);

        for (int i = 0; i < mViewArray.size(); i++) {
            View view = mViewArray.get(i);
            mSpringChain.addSpring(new SimpleSpringListener() {
                @Override
                public void onSpringUpdate(Spring spring) {
                    float value = (float) spring.getCurrentValue();
                    view.setScaleX(value);
                    view.setScaleY(value);
                }
            });
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG,"click view");
                }
            });
        }

        List<Spring> springs = mSpringChain.getAllSprings();
        for (int i = 0; i < springs.size(); i++) {
            springs.get(i).setCurrentValue(0.1);
        }

    }

    /**
     * 水平翻转
     *
     * @param view      target
     * @param duration  time
     * @param direction 只能传1或-1，1为从左开始翻转，-1位从右开始翻转
     * @return 动画集合
     */

    public static AnimatorSet flip(View view, int duration, int direction, int type) {
        if (direction != 1 && direction != -1) direction = 1;
        view.setCameraDistance(16000 * view.getResources().getDisplayMetrics().density);
        AnimatorSet animSet = new AnimatorSet();

        ObjectAnimator rotationY = new ObjectAnimator();
        rotationY.setDuration(duration).setPropertyName("rotationY");
        rotationY.setFloatValues(0, -90 * direction);

        ObjectAnimator _rotationY = new ObjectAnimator();
        _rotationY.setDuration(duration).setPropertyName("rotationY");
        _rotationY.setFloatValues(90 * direction, 0);
        _rotationY.setStartDelay(duration);

        ObjectAnimator scale = new ObjectAnimator();
        scale.setDuration(duration).setPropertyName("scaleY");
        scale.setFloatValues(1, 0.94f);

        ObjectAnimator _scale = new ObjectAnimator();
        _scale.setDuration(duration).setPropertyName("scaleY");
        _scale.setFloatValues(0.94f, 1);
        _scale.setStartDelay(duration);

        animSet.setTarget(view);
        rotationY.setTarget(view);
        _rotationY.setTarget(view);
        scale.setTarget(view);
        _scale.setTarget(view);
        switch (type) {
            case ROTATE_90: {
                animSet.playTogether(rotationY);
                break;
            }
            case ROTATE_90N: {
                animSet.playTogether(rotationY,_rotationY);
                break;
            }
            case ROTATE_90_HALF:{
                animSet.playTogether(_rotationY);
                break;
            }
        }

        animSet.start();
        return animSet;
    }
}
